﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace PnFramework.Unity
{
    public interface IObjectPool<T>
    {
        bool IsEmpty { get; }
        void Push(T o);
        void Get(string name, Action<T> callback = null);
    }
    public class GameObjectPool : IObjectPool<GameObject>
    {
        private Queue<GameObject> mPool = new Queue<GameObject>();
        public bool IsEmpty => mPool.Count == 0;
        public void Push(GameObject o)
        {
            o.SetActive(false);
            mPool.Enqueue(o);
        }
        public void Get(string name, Action<GameObject> callback = null)
        {
            if (mPool.Count > 0)
            {
                var o = mPool.Dequeue();
                o.SetActive(true);
                callback?.Invoke(o);
                return;
            }
            UnityResHelper.AsyncLoad<GameObject>(name, o =>
             {
                 o.name = name;
                 callback?.Invoke(o);
             });
        }
    }
}